<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */

function printMedalProgress() {
	// Let's gather all medal progress data and status here
	$progress = Array();
	$status = Array();
	$percentage = Array();

	$query = "SELECT * " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	
	$query2 = "SELECT count(*) " .
			"FROM ruler " .
			"WHERE economy_points > " . $row[ 'economy_points' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'economy_rank' ] = $row2[ 'count(*)' ] + 1;
	$percentage[ 'economy_rank' ] = '-';

	$query2 = "SELECT count(*) " .
			"FROM ruler " .
			"WHERE warrior_points > " . $row[ 'warrior_points' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'military_rank' ] = $row2[ 'count(*)' ] + 1; 
	$percentage[ 'military_rank' ] = '-';
	
	$query2 = "SELECT count(*) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'villages' ] = $row2[ 'count(*)' ];

	$query2 = "SELECT SUM(population) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'tax_payers' ] = $row2[ 'SUM(population)' ];

	$query2 = "SELECT SUM(infantry) " .
			"FROM army " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'infantry' ] = $row2[ 'SUM(infantry)' ];
	$query2 = "SELECT SUM(infantry) " .
			"FROM transfer " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'infantry' ] += $row2[ 'SUM(infantry)' ];
	$query2 = "SELECT infantry " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'infantry' ] += $row2[ 'infantry' ];
	
	$query2 = "SELECT SUM(cavalry) " .
			"FROM army " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'cavalry' ] = $row2[ 'SUM(cavalry)' ];
	$query2 = "SELECT SUM(cavalry) " .
			"FROM transfer " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'cavalry' ] += $row2[ 'SUM(cavalry)' ];
	$query2 = "SELECT cavalry " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'cavalry' ] += $row2[ 'cavalry' ];

	$query2 = "SELECT SUM(artillery) " .
			"FROM army " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'artillery' ] = $row2[ 'SUM(artillery)' ];
	$query2 = "SELECT SUM(artillery) " .
			"FROM transfer " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'artillery' ] += $row2[ 'SUM(artillery)' ];
	$query2 = "SELECT artillery " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$progress[ 'artillery' ] += $row2[ 'artillery' ];

	if( $progress[ 'economy_rank' ] < 26 && $progress[ 'economy_rank' ] > 10 ) {
		$status[ 'economy_bronze_star' ] = '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$status[ 'economy_silver_star' ] = '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$status[ 'economy_gold_star' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
	}
	elseif( $progress[ 'economy_rank' ] < 11 && $progress[ 'economy_rank' ] > 3 ) {
		$status[ 'economy_bronze_star' ] = translate( 'Exceeded' );
		$status[ 'economy_silver_star' ] = '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$status[ 'economy_gold_star' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
	}
	elseif( $progress[ 'economy_rank' ] < 4 ) {
		$status[ 'economy_bronze_star' ] = translate( 'Exceeded' );
		$status[ 'economy_silver_star' ] = translate( 'Exceeded' );
		$status[ 'economy_gold_star' ] 	= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
	}
	else {
		$status[ 'economy_bronze_star' ] = '<strong class="red">' . translate( 'Not Passed' ) . '</strong>';
		$status[ 'economy_silver_star' ] = '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$status[ 'economy_gold_star' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
	}

	if( $progress[ 'military_rank' ] < 26 && $progress[ 'military_rank' ] > 10 ) {
		$status[ 'military_bronze_star' ] = '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$status[ 'military_silver_star' ] = '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$status[ 'military_gold_star' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
	}
	elseif( $progress[ 'military_rank' ] < 11 && $progress[ 'military_rank' ] > 3 ) {
		$status[ 'military_bronze_star' ] = translate( 'Exceeded' );
		$status[ 'military_silver_star' ] = '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$status[ 'military_gold_star' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
	}
	elseif( $progress[ 'military_rank' ] < 4 ) {
		$status[ 'military_bronze_star' ] = translate( 'Exceeded' );
		$status[ 'military_silver_star' ] = translate( 'Exceeded' );
		$status[ 'military_gold_star' ] 	= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
	}
	else {
		$status[ 'military_bronze_star' ] = '<strong class="red">' . translate( 'Not Passed' ) . '</strong>';
		$status[ 'military_silver_star' ] = '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$status[ 'military_gold_star' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
	}

	if( $progress[ 'tax_payers' ] > 10000000 ) {
		$status[ 'peoples_protector_ribbon' ] 	= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$percentage[ 'peoples_protector_ribbon' ] = '100%';
	}
	else {
		$status[ 'peoples_protector_ribbon' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$percentage[ 'peoples_protector_ribbon' ] = round( ( $progress[ 'tax_payers' ] / 10000000 ) * 100 ) . '%';
	}

	if( $progress[ 'villages' ] >= 40 ) {
		$status[ 'grand_expander_medal' ] 	= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$status[ 'expander_medal' ] 			= translate( 'Exceeded' );
		$percentage[ 'grand_expander_medal' ] = '100%';
		$percentage[ 'expander_medal' ] = '-';
	}
	elseif( $progress[ 'villages' ] >= 20 ) {
		$status[ 'grand_expander_medal' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$status[ 'expander_medal' ] 			= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$percentage[ 'grand_expander_medal' ] = round( ( $progress[ 'villages' ] / 40 ) * 100 ) . '%';
		$percentage[ 'expander_medal' ] = '100%';
	}
	else {
		$status[ 'grand_expander_medal' ] 	= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$status[ 'expander_medal' ] 			= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$percentage[ 'grand_expander_medal' ] = round( ( $progress[ 'villages' ] / 40 ) * 100 ) . '%';
		$percentage[ 'expander_medal' ] = round( ( $progress[ 'villages' ] / 20 ) * 100 ) . '%';;
	}
	
	if( $progress[ 'infantry' ] > 4000000 ) {
		$status[ 'infantry' ] 			= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$percentage[ 'infantry' ] 		= '100%';
	}
	else {
		$status[ 'infantry' ] 			= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$percentage[ 'infantry' ] = round( ( $progress[ 'infantry' ] / 4000000 ) * 100 ) . '%';
	}

	if( $progress[ 'cavalry' ] > 400000 ) {
		$status[ 'cavalry' ] 			= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$percentage[ 'cavalry' ] 		= '100%';
	}
	else {
		$status[ 'cavalry' ] 			= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$percentage[ 'cavalry' ] = round( ( $progress[ 'cavalry' ] / 400000 ) * 100 ) . '%';
	}

	if( $progress[ 'artillery' ] > 400000 ) {
		$status[ 'artillery' ] 			= '<strong class="green">' . translate( 'Passed' ) . '</strong>';
		$percentage[ 'artillery' ] 		= '100%';
	}
	else {
		$status[ 'artillery' ] 			= '<strong class="red">' . translate( 'Not passed' ) . '</strong>';
		$percentage[ 'artillery' ] = round( ( $progress[ 'artillery' ] / 400000 ) * 100 ) . '%';
	}

?>
				<div class="text-bubble-container">
					<div class="text-bubble-header">
						<div class="text-bubble-left-top"></div>
						<div class="text-bubble-filler"></div>
						<div class="text-bubble-right-top"></div>
						<div class="clear"></div>
					</div>
					<div class="text-bubble-content">
						<?php transprint( 'You have completed all available tasks, you can now try to gather all these awards and decorations.' ); ?><br />
						<br />
						<a href="/aosmanual/medals.php"><strong><?php transprint( 'Awards and decorations' ); ?></strong></a><br />
						<table id="final-tasks">
							<tr>
								<th align="left">
									&nbsp;
								</th>
								<th align="left">
									<?php transprint( 'Title' ); ?>
								</th>
								<th align="right">
									<?php transprint( 'Progress' ); ?>
								</th>
								<th>
									&nbsp;&nbsp;&nbsp;
								</th>
								<th align="left">
									<?php transprint( 'Requirement' ); ?>
								</th>
								<th align="right">
									<?php transprint( 'Percentage' ); ?>
								</th>
								<th align="right">
									<?php transprint( 'Status' ); ?>
								</th>
							</tr>
							<tr class="even">
								<td>
									<div class="medal economic-bronze-star"></div>
								</td>
								<td>
									<?php transprint( 'Economic bronze star' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'economy_rank' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '11 - 25 ' . translate( 'economy rank' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'economy_rank' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'economy_bronze_star' ] ); ?>
								</td>
							</tr>
							<tr class="odd">
								<td>
									<div class="medal economic-silver-star"></div>
								</td>
								<td>
									<?php transprint( 'Economic silver star' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'economy_rank' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '4 - 10 ' . translate( 'economy rank' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'economy_rank' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'economy_silver_star' ] ); ?>
								</td>
							</tr>
							<tr class="even">
								<td>
									<div class="medal economic-gold-star"></div>
								</td>
								<td>
									<?php transprint( 'Economic gold star' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'economy_rank' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '1 - 3 ' . translate( 'economy rank' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'economy_rank' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'economy_gold_star' ] ); ?>
								</td>
							</tr>
							<tr class="odd">
								<td>
									<div class="medal military-bronze-star"></div>
								</td>
								<td>
									<?php transprint( 'Military bronze star' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'military_rank' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '11 - 25 ' . translate( 'war rank' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'military_rank' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'military_bronze_star' ] ); ?>
								</td>
							</tr>
							<tr class="even">
								<td>
									<div class="medal military-silver-star"></div>
								</td>
								<td>
									<?php transprint( 'Military silver star' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'military_rank' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '4 - 10 ' . translate( 'war rank' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'military_rank' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'military_silver_star' ] ); ?>
								</td>
							</tr>
							<tr class="odd">
								<td>
									<div class="medal military-gold-star"></div>
								</td>
								<td>
									<?php transprint( 'Military gold star' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'military_rank' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '1 - 3 ' . translate( 'war rank' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'military_rank' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'military_gold_star' ] ); ?>
								</td>
							</tr>
							<tr class="even">
								<td>
									<div class="medal peoples-protector-ribbon"></div>
								</td>
								<td>
									<?php transprint( 'Peoples protector ribbon' ); ?>
								</td>
								<td align="right">
									<strong class="population"><?php print( number_format( $progress[ 'tax_payers' ] ) ); ?></strong>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '<strong class="population">10,000,000</strong> ' . translate( 'population' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'peoples_protector_ribbon' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'peoples_protector_ribbon' ] ); ?>
								</td>
							</tr>
							<tr class="odd">
								<td>
									<div class="medal expander-medal"></div>
								</td>
								<td>
									<?php transprint( 'Expander medal' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'villages' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '20 ' . translate( 'villages' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'expander_medal' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'expander_medal' ] ); ?>
								</td>
							</tr>
							<tr class="even">
								<td>
									<div class="medal grand-expander-medal"></div>
								</td>
								<td>
									<?php transprint( 'Grand expander medal' ); ?>
								</td>
								<td align="right">
									<?php print( number_format( $progress[ 'villages' ] ) ); ?>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<?php print( '40 ' . translate( 'villages' ) ); ?>
								</td>
								<td align="right">
									<?php print( $percentage[ 'grand_expander_medal' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'grand_expander_medal' ] ); ?>
								</td>
							</tr>
							<tr class="odd">
								<td>
									<div class="medal infantry-branch-insignia"></div>
								</td>
								<td>
									<?php transprint( 'Infantry branch insignia *' ); ?>
								</td>
								<td align="right">
									<strong class="infantry"><?php print( number_format( $progress[ 'infantry' ] ) ); ?></strong>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<strong class="infantry"><?php print( '4,000,000</strong> ' . translate( 'infantry' ) ); ?></strong>
								</td>
								<td align="right">
									<?php print( $percentage[ 'infantry' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'infantry' ] ); ?>
								</td>
							</tr>
							<tr class="even">
								<td>
									<div class="medal cavalry-branch-insignia"></div>
								</td>
								<td>
									<?php transprint( 'Cavalry branch insignia *' ); ?>
								</td>
								<td align="right">
									<strong class="cavalry"><?php print( number_format( $progress[ 'cavalry' ] ) ); ?></strong>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<strong class="cavalry"><?php print( '400,000</strong> ' . translate( 'cavalry' ) ); ?></strong>
								</td>
								<td align="right">
									<?php print( $percentage[ 'cavalry' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'cavalry' ] ); ?>
								</td>
							</tr>
							<tr class="odd">
								<td>
									<div class="medal artillery-branch-insignia"></div>
								</td>
								<td>
									<?php transprint( 'Artillery branch insignia *' ); ?>
								</td>
								<td align="right">
									<strong class="artillery"><?php print( number_format( $progress[ 'artillery' ] ) ); ?></strong>
								</td>
								<td>
									&nbsp;&nbsp;&nbsp;
								</td>
								<td align="left">
									<strong class="infantry"><?php print( '400,000</strong> ' . translate( 'artillery' ) ); ?></strong>
								</td>
								<td align="right">
									<?php print( $percentage[ 'artillery' ] ); ?>
								</td>
								<td align="right">
									<?php print( $status[ 'artillery' ] ); ?>
								</td>
							</tr>
						</table>
						<br />
						<?php transprint( 'Note: Will be awarded at the end of the round with the exception of the ones with a *' ); ?><br />
						* <?php transprint( 'Will only be awarded once' ); ?>
					</div>
					<div class="text-bubble-footer">
						<div class="text-bubble-left-bottom"></div>
						<div class="text-bubble-filler"></div>
						<div class="text-bubble-right-bottom"></div>
						<div class="clear"></div>
					</div>
					<div class="text-bubble-point-container">
						<div class="text-bubble-point"></div>
						<div class="clear"></div>
					</div>
				</div>
				<div class="task-soldier"></div>
				<div class="clear"></div>
<?php
}

function printCurrentTask() {
	global $dal;

	$statement = $dal->prepare( 'SELECT * ' .
			'FROM ruler_task ' .
			'JOIN tasks ON tasks.task_id = ruler_task.task_id ' .
			'WHERE ruler_id = :ruler_id AND completed = 0' );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$task = $dal->getObject( $statement );
?>
				<div class="text-bubble-container">				
					<div class="text-bubble-header">
						<div class="text-bubble-left-top"></div>
						<div class="text-bubble-filler"></div>
						<div class="text-bubble-right-top"></div>
						<div class="clear"></div>
					</div>
					<div class="text-bubble-content">
						<strong><?php transprint( $task->title ); ?></strong><br />
						<div class="text"><?php transprint( $task->description ); ?></div>
<?php
	if( $task->finished == 0 ) { 
		if( $task->required_building != 0 ) {
			$statement = $dal->prepare( 'SELECT name ' .
					'FROM buildings ' .
					'WHERE building_id = :building_id' );
			$statement->bindParam( ':building_id', $task->required_building, PDO::PARAM_INT );
			$title = translate( $dal->getValue( $statement ) );
			$tooltip = getBuildingTooltip( $task->required_building );
?>
						<br />
						<?php transprint( 'Construct' ); ?>: <a href="../villages/building.php?id=<?php print( $task->required_building ); ?>" title="<?php print( $tooltip ); ?>" class="make-yui-tooltip"><?php print( $title ); ?></a><br />
						<br />
<?php			
		}
		elseif( $task->required_upgrade != 0 ) {
			$statement = $dal->prepare( 'SELECT name ' .
					'FROM upgrades ' .
					'WHERE upgrade_id = :upgrade_id' );
			$statement->bindParam( ':upgrade_id', $task->required_upgrade, PDO::PARAM_INT );
			$title = translate( $dal->getValue( $statement ) );
?>
						<br />
						<?php transprint( 'Upgrade' ); ?>: <a href="../upgrade/upgradeinfo.php?id=<?php print( $task->required_upgrade ); ?>"><?php print( $title ); ?></a><br />
						<br />
<?php			
		}
		elseif( $task->required_scouts != 0 ) {
?>
						<br />
						<?php transprint( 'Train' ); ?>: <strong class="scouts"><?php print( number_format( $task->required_scouts ) ); ?></strong> <?php transprint( 'scouts' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_infantry != 0 ) {
?>
						<br />
						<?php transprint( 'Train' ); ?>: <strong class="infantry"><?php print( number_format( $task->required_infantry ) ); ?></strong> <?php transprint( 'infantry' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_cavalry != 0 ) {
?>
						<br />
						<?php transprint( 'Train' ); ?>: <strong class="cavalry"><?php print( number_format( $task->required_cavalry ) ); ?></strong> <?php transprint( 'cavalry' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_artillery != 0 ) {
?>
						<br />
						<?php transprint( 'Train' ); ?>: <strong class="artillery"><?php print( number_format( $task->required_artillery ) ); ?></strong> <?php transprint( 'artillery' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_officer != 0 ) {
?>
						<br />
						<?php transprint( 'Recruit an officer' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_gold != 0 ) {
?>
						<br />
						<?php transprint( 'Gather' ); ?>: <strong class="gold"><?php print( number_format( $task->required_gold ) ); ?></strong> <?php transprint( 'gold' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_iron != 0 ) {
?>
						<br />
						<?php transprint( 'Gather' ); ?>: <strong class="iron"><?php print( number_format( $task->required_iron ) ); ?></strong> <?php transprint( 'iron' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_coal != 0 ) {
?>
						<br />
						<?php transprint( 'Gather' ); ?>: <strong class="coal"><?php print( number_format( $task->required_coal ) ); ?></strong> <?php transprint( 'coal' ); ?><br />
						<br />
<?php			
		}
		elseif( $task->required_recruits != 0 ) {
?>
						<br />
						<?php transprint( 'Gather' ); ?>: <strong class="recruits"><?php print( number_format( $task->required_recruits ) ); ?> <?php transprint( 'recruits' ); ?></strong><br />
						<br />
<?php			
		}
		elseif( $task->required_population != 0 ) {
?>
						<br />
						<?php transprint( 'Increase population to' ); ?>: <strong class="population"><?php print( number_format( $task->required_population ) ); ?> <?php transprint( 'people' ); ?></strong><br />
						<br />
<?php			
		}
		elseif( $task->required_villages != 0 ) {
?>
						<br />
						<?php transprint( 'Control' ); ?>: <?php print( number_format( $task->required_villages ) ); ?> <?php transprint( 'villages' ); ?><br />
						<br />
<?php
		}
		elseif( $task->required_food != 0 ) {
?>
						<br />
						<?php transprint( 'Gather' ); ?>: <strong class="food"><?php print( number_format( $task->required_food ) ); ?></strong> <?php transprint( 'food' ); ?><br />
						<br />
<?php
		}
	}
	
	if( $task->reward_gold > 0 ) {
?>
						<?php transprint( 'Reward' ); ?>: <strong class="gold"><?php print( number_format( $task->reward_gold ) ); ?></strong> <?php transprint( 'gold' ); ?><br />
<?php		
	}

	if( $task->reward_premium_turns > 0 ) {
?>
						<?php transprint( 'Reward' ); ?>: <?php print( number_format( $task->reward_premium_turns ) ); ?> <?php transprint( 'premium turns' ); ?><br />
<?php
	}
	
//	if( $task->reward_premium_turns > 0 || $task->reward_gold > 0 ) {
//	}
	if( $task->required_capital == 1 || $task->next_task2 != -1 || $task->finished == 1 ) { 
?>				
						<br />
						<form method="post" action="javascript:AOS.tasks.submitTask();" id="submit-task-form">
<?php
	}
	if( $task->finished == 1 ) {
?>
							<?php transprint( 'You have completed this task, click confirm to receive your reward.' ); ?><br />
<?php		
	}
	elseif( $task->next_task2 != -1 ) {
		$query2 = "SELECT title " .
				"FROM tasks " .
				"WHERE task_id = " . $task->next_task;
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$title = $row2[ 'title' ];
		$query2 = "SELECT title " .
				"FROM tasks " .
				"WHERE task_id = " . $task->next_task2;
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$title2 = $row2[ 'title' ];
?>
							<input name="next_task" id="next-task-1" type="radio" value="<?php print( $task->next_task ); ?>"> <label for="next-task-1"><?php transprint( $title ); ?></label><br />
							<input name="next_task" id="next-task-2" type="radio" value="<?php print( $task->next_task2 ); ?>"> <label for="next-task-2"><?php transprint( $title2 ); ?></label><br />
<?php
		if( $task->next_task3 != -1 ) {
			$query2 = "SELECT title " .
					"FROM tasks " .
					"WHERE task_id = " . $task->next_task3;
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$title3 = $row2[ 'title' ];
?>
							<input name="next_task" id="next-task-3" type="radio" value="<?php print( $task->next_task3 ); ?>"> <label for="next-task-3"><?php transprint( $title3 ); ?></label><br />
<?php
		}
	}
	elseif( $task->required_capital == 1 ) {
		if( isset( $_POST[ 'capital_x' ] ) && $_POST[ 'capital_x' ] != -1 ) {
			$capitalX = htmlentities( $_POST[ 'capital_x' ] );
		}
		else {
			$capitalX = '';
		}
		if( isset( $_POST[ 'capital_y' ] ) && $_POST[ 'capital_y' ] != -1 ) {
			$capitalY = htmlentities( $_POST[ 'capital_y' ] );
		}
		else {
			$capitalY = '';
		}
?>
							<?php transprint( 'Capital location' ); ?> (x, y):<br />
							<input id="capital-x" type="text" size="4" value="<?php print( $capitalX ); ?>">, <input id="capital-y" type="text" value="<?php print( $capitalY ); ?>" size="4">&nbsp; 
<?php
	}
	
	if( $task->required_capital == 1 || $task->next_task2 != -1 || $task->finished == 1 ) { 
?>
							<input type="submit" value="<?php transprint( 'confirm' ); ?>" /><br />
						</form>
<?php
	}
	if( getCurrentTurn() == 1 ) {
?>
						<strong><?php transprint( 'Note' ); ?></strong>: <?php transprint( 'Turns have not started yet' ); ?>.<br />
<?php		
	}
?>
					</div>
					<div class="text-bubble-footer">
						<div class="text-bubble-left-bottom"></div>
						<div class="text-bubble-filler"></div>
						<div class="text-bubble-right-bottom"></div>
						<div class="clear"></div>
					</div>
					<div class="text-bubble-point-container">
						<div class="text-bubble-point"></div>
						<div class="clear"></div>
					</div>
				</div>
				<div class="task-soldier"></div>
				<div class="clear"></div>
<?php
}

function completeTask( $task, $nextTask = -1 ) {
	global $dal, $premium;

	$date = getCurrentDate();
	$turn = getCurrentTurn();
	
	if( $nextTask == -1 ) {
		$nextTask = $task->next_task;
	}

	// If there are any rewards, we hand those out here
	if( $task->reward_gold > 0 || $task->reward_premium_turns > 0 ) {
		$title = translate( 'Reward for completing a task' );
		$news = translate( 'For completing the task' ) . ' "' . translate( $task->title ) . 
				'" ' . translate( 'you received' . ":\n" );
		if( $task->reward_gold > 0 ) {
			$news .=	'<strong class="gold">' . number_format( $task->reward_gold ) . '</strong> ' . translate( 'gold' );
			$statement = $dal->prepare( 'UPDATE ruler ' .
					'SET gold = gold + :gold_reward ' .
					'WHERE ruler_id = :ruler_id' );
			$statement->bindParam( ':gold_reward', $task->reward_gold, PDO::PARAM_INT );
			$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
			$dal->execute( $statement );
		}
		if( $task->reward_premium_turns > 0 ) {
			if( $task->reward_gold > 0 ) {
				$news .= "\n";
			}
			$news .=	number_format( $task->reward_premium_turns ) . ' ' . translate( 'premium turns' );
			if( $premium == -1 ) {
				$task->reward_premium_turns ++;
			}
			
			$statement = $dal->prepare( 'UPDATE users ' .
					'SET premium = ( premium + :premium_reward ) ' .
					'WHERE user_id = :user_id' );
			$statement->bindParam( ':premium_reward', $task->reward_premium_turns, PDO::PARAM_INT );
			$statement->bindParam( ':user_id', $_SESSION[ 'user_id' ], PDO::PARAM_INT );
			$dal->execute( $statement );
		}
		
		$statement = $dal->prepare( 'INSERT INTO news ' .
				'( `ruler_id`, `type`, `news`, `date`, `subject`, `turn` ) ' .
				'VALUES( :ruler_id, 1, :news, :date, :title, :turn )' );
		$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
		$statement->bindParam( ':news', $news, PDO::PARAM_STR );
		$statement->bindParam( ':date', $date, PDO::PARAM_STR );
		$statement->bindParam( ':title', $title, PDO::PARAM_STR );
		$statement->bindParam( ':turn', $turn, PDO::PARAM_INT );
		$dal->execute( $statement );
	}

	// Update tasks
	$statement = $dal->prepare( 'UPDATE ruler_task ' .
			'SET completed = 1, completed_turn = :turn, completed_date = :date ' .
			'WHERE task_id = :task_id AND ruler_id = :ruler_id' );
	$statement->bindParam( ':turn', $turn, PDO::PARAM_INT );
	$statement->bindParam( ':date', $date, PDO::PARAM_STR );
	$statement->bindParam( ':task_id', $task->task_id, PDO::PARAM_INT );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$dal->execute( $statement );

	if( $nextTask != -1 ) {
		$statement = $dal->prepare( 'INSERT INTO ruler_task ' .
				'( `ruler_id`, `task_id` ) ' .
				'VALUES( :ruler_id, :task_id )' );
		$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
		$statement->bindParam( ':task_id', $nextTask, PDO::PARAM_INT );
		$dal->execute( $statement );
	}
}
?>
